﻿using System;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using Behemoth.SharePoint;

namespace Behemoth.SharePoint.LoggedCommentField {

  // TODO: Replace, as needed, "TextField" with some other class derived from Microsoft.SharePoint.WebControls.BaseFieldControl.
  [CLSCompliant(false)]
  [Guid("8acd8cc3-86be-4629-b953-8699aa89af5d")]
  public class LoggedComment_RichTextField : RichTextField {

    protected TextBox textField;
    protected HtmlInputHidden textField_spSave;
    protected PlaceHolder CommentHistoryPlaceHolder;

    protected LoggedCommentField FieldTyped {
      get { return this.Field as LoggedCommentField; }
    }

    protected override string DefaultTemplateName {
      get {
        if (this.FieldTyped.EditRichText) {
          return "RichTextField";
        } else {
          return "NoteField";
        }
      }
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);
      SPPresenceTools.AddFixPresenceScript();
    }

    public override void RenderControl(HtmlTextWriter writer) {
      if (!this.FieldTyped.RichText || !this.FieldTyped.EditRichText) {
        base.RenderControl(writer);
      }
      SPRichTextMode mode = this.FieldTyped.RichTextMode;
      if (this.FieldTyped.EditRichTextMode == "Compatible")
        this.FieldTyped.RichTextMode = SPRichTextMode.Compatible;
      else
        this.FieldTyped.RichTextMode = SPRichTextMode.FullHtml;
      base.RenderControl(writer);
      this.FieldTyped.RichTextMode = mode;
    }

    protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output) {
      base.RenderFieldForDisplay(output);
    }
    protected override void RenderFieldForInput(System.Web.UI.HtmlTextWriter output) {
      base.RenderFieldForInput(output);
    }

    public override object Value {
      get {
        EnsureChildControls();
        string comment = this.textField.Text;
        LoggedCommentFieldValue fv = ItemFieldValueTyped;
        if (fv == null) {
          fv = new LoggedCommentFieldValue(new Data.Comments(), this.FieldTyped);
          this.ItemFieldValue = fv; // not 100% convinced this line of code is needed here
        }
        fv.AddComment(this.Web.CurrentUser, comment);
        return fv;
        //string s = LoggedCommentFunctions.AppendNewComment(this.FieldTyped, this, this.textField);
        //return s;
      }
      set {
        EnsureChildControls();
        textField.Text = string.Empty; //(string)this.ItemFieldValue;
      }
    }

    protected override void CreateChildControls() {
      if (Field == null) return;
      base.CreateChildControls();
      if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
        return;
      this.textField_spSave = LoggedCommentFunctions.SetupHiddenControl(this.FieldTyped, this);
      this.textField = LoggedCommentFunctions.SetupTextControl(this.FieldTyped, this);
      this.CommentHistoryPlaceHolder = LoggedCommentFunctions.AddCommentControl(this.FieldTyped, this);
    }

    /// <summary>
    /// Overriden to ensure that objects are always converted to 
    /// LoggedCommentFieldValue type before they are stored and retreived.
    /// </summary>
    public override object ItemFieldValue {
      get { return LoggedCommentFieldValue.UpConvertMultiColumnValue(base.ItemFieldValue); }
      set { base.ItemFieldValue = LoggedCommentFieldValue.UpConvertMultiColumnValue(value); }
    }

    public LoggedCommentFieldValue ItemFieldValueTyped {
      get { return this.ItemFieldValue as LoggedCommentFieldValue; }
    }

    /// <summary>
    /// Overriden to ensure that objects are always converted to 
    /// LoggedCommentFieldValue type before they are stored and retreived.
    /// </summary>
    public override object ListItemFieldValue {
      get { return LoggedCommentFieldValue.UpConvertMultiColumnValue(base.ListItemFieldValue); }
      set { base.ListItemFieldValue = LoggedCommentFieldValue.UpConvertMultiColumnValue(value); }
    }

    public LoggedCommentFieldValue ListItemFieldValueTyped {
      get { return this.ListItemFieldValue as LoggedCommentFieldValue; }
    }

  } // class

  // TODO: Replace, as needed, "TextField" with some other class derived from Microsoft.SharePoint.WebControls.BaseFieldControl.
  [CLSCompliant(false)]
  [Guid("11E8D36D-F074-4bf5-9799-DB7A799409BA")]
  public class LoggedComment_NoteField : NoteField {

    protected TextBox textField;
    protected HtmlInputHidden textField_spSave;
    protected PlaceHolder CommentHistoryPlaceHolder;

    protected LoggedCommentField FieldTyped {
      get { return this.Field as LoggedCommentField; }
    }

    protected override string DefaultTemplateName {
      get {
        if (this.FieldTyped.EditRichText) {
          return "RichTextField";
        } else {
          return "NoteField";
        }
      }
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);
      SPPresenceTools.AddFixPresenceScript();
    }

    protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output) {
      base.RenderFieldForDisplay(output);
    }
    protected override void RenderFieldForInput(System.Web.UI.HtmlTextWriter output) {
      base.RenderFieldForInput(output);
    }

    public override object Value {
      get {
        EnsureChildControls();
        string comment = this.textField.Text;
        LoggedCommentFieldValue fv = ItemFieldValueTyped;
        if (fv == null) {
          fv = new LoggedCommentFieldValue(new Data.Comments(), this.FieldTyped);
          this.ItemFieldValue = fv; // not 100% convinced this line of code is needed here
        }
        fv.AddComment(this.Web.CurrentUser, comment);
        return fv; 
        //string s = LoggedCommentFunctions.AppendNewComment(this.FieldTyped, this, this.textField);
        //return s;
        /*
        EnsureChildControls();
        string s = LoggedCommentFunctions.AppendNewComment(this.FieldTyped, this, this.textField);
        return s;
         */
      }
      set {
        EnsureChildControls();
        textField.Text = string.Empty; //(string)this.ItemFieldValue;
      }
    }

    protected override void CreateChildControls() {
      if (Field == null) return;
      base.CreateChildControls();
      if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
        return;
      this.textField_spSave = LoggedCommentFunctions.SetupHiddenControl(this.FieldTyped, this);
      this.textField = LoggedCommentFunctions.SetupTextControl(this.FieldTyped, this);
      this.CommentHistoryPlaceHolder = LoggedCommentFunctions.AddCommentControl(this.FieldTyped, this);
    }

    /// <summary>
    /// Overriden to ensure that objects are always converted to 
    /// LoggedCommentFieldValue type before they are stored and retreived.
    /// </summary>
    public override object ItemFieldValue {
      get { return LoggedCommentFieldValue.UpConvertMultiColumnValue(base.ItemFieldValue); }
      set { base.ItemFieldValue = LoggedCommentFieldValue.UpConvertMultiColumnValue(value); }
    }

    public LoggedCommentFieldValue ItemFieldValueTyped {
      get { return this.ItemFieldValue as LoggedCommentFieldValue; }
    }

    /// <summary>
    /// Overriden to ensure that objects are always converted to 
    /// LoggedCommentFieldValue type before they are stored and retreived.
    /// </summary>
    public override object ListItemFieldValue {
      get { return LoggedCommentFieldValue.UpConvertMultiColumnValue(base.ListItemFieldValue); }
      set { base.ListItemFieldValue = LoggedCommentFieldValue.UpConvertMultiColumnValue(value); }
    }

    public LoggedCommentFieldValue ListItemFieldValueTyped {
      get { return this.ListItemFieldValue as LoggedCommentFieldValue; }
    }

  } // class

}
